from bs4 import BeautifulSoup
import douban
import csv

html = ''
names = []
directors=[]
actors = []
years = []
countries = []
types = []
#取图片地址
srcs = []
'''
导演: 弗洛里安·亨克尔·冯·多纳斯马尔克 Florian Henckel von Donnersmarck  &amp;n...<br/>
                            2006 / 德国 / 剧情 悬疑 1
'''
#把所有数据集加进去
for i in range(1,11):
    with open(f'top250/src{i}.html','r',encoding='utf-8') as f:
        html = f.read() #把所有内容读取到html中
        # print(html)
        bs = BeautifulSoup(html,'lxml')
        # print(f"i:{i}")
        # print(bs.select('div.item div.hd span.title:nth-child(1)'))
        douban.setTop250Data(bs,names,directors,actors,years,countries,types,srcs)

# print(names)
# print(len(srcs))
# print(len(directors))
# print(len(actors))
# print(len(years))
# print(len(countries))
# print(len(types))

def write_csv():
    with open('data/movie.csv','w',encoding='utf-8',newline='') as f:
        fieldnames = ['名字','导演','演员','年份','国家','类型','图片地址']
        writer = csv.DictWriter(f,fieldnames=fieldnames)
        writer.writeheader()
        for n,d,a,y,c,t,s in zip(names,directors,actors,years,countries,types,srcs):
            writer.writerow({'名字':n,'导演':d,'演员':a,'年份':y,'国家':c,'类型':t,'图片地址':s})

def write_img():
    with open('data/movie.csv','r',encoding='utf-8') as f:
        reader = csv.DictReader(f)
        # 001 002
        index = 1
        for row in reader:
            fname = row['名字']
            print(f'{index:03}_{fname}')
            douban.handlerImg(f'{index:03}_{fname}',row['图片地址'])
            index+=1

write_img()